<?php
namespace app\manage\model;

use think\Model;

class TaskModel extends Model{
	//表名
	protected $table = 'ly_task';

	/**
	 * 碎片化NFT
	 */
	public function add(){
		if (!request()->isAjax()) return '提交失败';
		$param = input('param.');
		//数据验证
		// $validate = validate('app\manage\validate\Task');
		// if (!$validate->scene('add')->check($param)) return $validate->getError();



		if (isset($param['finish_condition']) and $param['finish_condition']) $param['finish_condition'] 	= json_encode(array_keys($param['finish_condition']));

		if (isset($param['task_step']) and $param['task_step']) $param['task_step']               			= json_encode(array_merge($param['task_step']),true);
		if (isset($param['examine_demo']) and $param['examine_demo']) $param['examine_demo']         		= json_encode($param['examine_demo'],true);

		$nft_id= request()->param('nft_id',0,'intval') ;
		$nftInfo=db('nft')->where(['id'=>$nft_id])->find();
		if(empty($nftInfo)){
			return 'NFT不存在';
		}

		if($nftInfo['status'] !=1 || $nftInfo['state']!=1 ||$nftInfo['is_move']!=0 ){
			return 'NFT状态错误';
		}
		if(!in_array($nftInfo['from'],[1,2])){
			return 'NFT来源不能碎片化';
		}



		//$param['end_time']                                         = strtotime($param['end_time']);
		$param['add_time']                                         = time();
		$param['surplus_number']								   = $param['total_number'];
		$param['total_price']=$param['total_number']*$param['reward_price'];
        $end_time = 0;
        if (isset($param['cost_hours'])&&$param['cost_hours']>0){
            $end_time = time()+intval($param['cost_hours'])*60*60;
        }
		$param['end_time'] = $end_time ;
		$param['uids'] = request()->param('uids','','trim') ;
		if(!isset($param['credit_type']) || $param['credit_type']==0){
			$param['credit_type']=$nftInfo['credit_type'];
		}
		$param['compound_num'] =  request()->param('total_number',0,'intval') ;
		$param['status']= 1;

		// 流水 任务金额

		$param['order_number']								 = 'B'.trading_number();
		$param['trade_number']								 = 'L'.trading_number();
		//$param['username']									 = '1'.mt_rand(50,99).'3745'.mt_rand(1483,9789);
		if(isset($param['nft_id'])){
			$param['uid']=db('nft')->where(['id'=>$param['nft_id']])->value('uid');
			$param['title']=db('nft')->where(['id'=>$param['nft_id']])->value('name');
		}


        $param['task_class'] = 1;

		$res = $this->allowField(true)->save($param);
		if(isset($param['nft_id'])){
			db('nft')->where(['id'=>$param['nft_id']])->update([
				'state'=>2,
				'reward_price'=>$param['reward_price'],
				'recovery_price'=>$param['recovery_price'],
				'release_price'=>$param['release_price'],
				'total_number'=>$param['total_number'],
				'compound_num'=>$param['compound_num']
			]);
			//给自己添加nft碎片
			db('users_goods')->insert([
				'uid'=>isset($param['uid'])?$param['uid']:0,
				'nft_id'=>$nft_id,
				'total_number'=>$param['total_number'],
				'status'=>1,
				'reward_price'=>$param['reward_price'],
				'recovery_price'=>$param['recovery_price'],
				'release_price'=>$param['release_price'],
				'compound_num'=>$param['compound_num'],
				'create_time'=>time(),
		    ]);
		}

		if (!$res) return '添加失败';

		//添加操作日志
		model('Actionlog')->actionLog(session('manage_username'),'拆分碎片：nft为'.$param['title'],1);

		return 1;
	}

	/**
	 * 编辑任务
	 */
	public function edit(){

		if (!request()->isAjax()) return '提交失败';

		$param = input('param.');

		if (isset($param['type']) && $param['type']){
			$res = $this->where('id',$param['id'])->update(array('is_show'=>$param['value']));
			if (!$res) return '修改失败';
			return 1;
		}
		//数据验证
		$validate = validate('app\manage\validate\Task');
		if (!$validate->scene('add')->check($param)) return $validate->getError();

		$id = $param['id'];
		unset($param['id']);
		if (isset($param['finish_condition']) && $param['finish_condition']) $param['finish_condition'] = json_encode(array_keys($param['finish_condition']));
		if (isset($param['examine_demo']) && $param['examine_demo']) $param['examine_demo']         	= json_encode($param['examine_demo'],true);
		if (isset($param['task_step']) && $param['task_step']) $param['task_step']               		= json_encode(array_merge($param['task_step']),true);
		$param['end_time']                                         = strtotime($param['end_time']);

		$taskInfo	= $this->where('id', $id)->find();
		if(!$taskInfo){
			if($param['lang']=='cn') return ['code'=>0, 'code_dec'=>'任务不存在'];
			else return ['code'=>0, 'code_dec'=>'Task does not exist!'];
		}

		// 如果是修改任务的领取数量，则必须修改剩余数量——————————————————————————————
		if($param['total_number'] && $param['total_number'] < $taskInfo['total_number']){	// 判断新数量必须大于原数量
			if($param['lang']=='cn') return '新的领取数量应大于原来的领取数量';
			else return 'The new collection quantity should be greater than the original collection quantity!';
		}

		if($param['total_number'] && $param['total_number'] > $taskInfo['total_number']){
			$param['surplus_number']	= $param['total_number'] - $taskInfo['receive_number'];
		}

		$res = $this->allowField(true)->save($param, ['id'=>$id]);
		if (!$res) return '修改失败';
		//添加操作日志
		model('Actionlog')->actionLog(session('manage_username'),'修改任务：标题为'.$param['title'],1);

		return 1;
	}


	/**
	 * 编辑任务
	 */
	public function del(){
		if (!request()->isAjax()) return '提交失败';
		$param = input('param.');
		if (!$param) return '提交失败';

		if (isset($param['data']) && $param['data']) { // 批量删除
			foreach ($param['data'] as $key => $value) {
				// 提取信息
				$taskInfo = $this->where('id', $value['id'])->find();
				if ($taskInfo && is_object($taskInfo)) $taskInfo = $taskInfo->toArray();
				// 删除图片
				if ($taskInfo['examine_demo']) {
					$taskInfo['examine_demo'] = json_decode($taskInfo['examine_demo'], true);
					foreach ($taskInfo['examine_demo'] as $key => $value) {
						@unlink('.'.$value);
					}
				}
				// 删除图片
				if ($taskInfo['task_step']) {
					$taskInfo['task_step'] = json_decode($taskInfo['task_step'], true);
					foreach ($taskInfo['task_step'] as $key => $value) {
						@unlink('.'.$value['img']);
					}
				}

				$res[] = $this->where('id', $value['id'])->delete();
			}
		} elseif (isset($param['id']) && $param['id']) { // 删除单个
			// 提取信息
			$taskInfo = $this->where('id', $param['id'])->find();
			if ($taskInfo && is_object($taskInfo)) $taskInfo = $taskInfo->toArray();

			// 删除图片
			if ($taskInfo['examine_demo']) {
				$taskInfo['examine_demo'] = json_decode($taskInfo['examine_demo'], true);
				foreach ($taskInfo['examine_demo'] as $key => $value) {
					@unlink('.'.$value);
				}
			}
			// 删除图片
			if ($taskInfo['task_step']) {
				$taskInfo['task_step'] = json_decode($taskInfo['task_step'], true);
				foreach ($taskInfo['task_step'] as $key => $value) {
					@unlink('.'.$value['img']);
				}
			}

			$res = $this->where('id', $param['id'])->delete();
			if (!$res) return '删除失败';
		} else {
			return '提交失败';
		}

		return 1;
	}

	/**
	 * 审核
	 * @return [type] [description]
	 */
	public function audit(){
		if (!request()->isAjax()) return '提交失败';

		$param = input('param.');
		if (!$param || !isset($param['id']) || !$param['id']) return '提交失败';

		$updateArray = [];

		if (isset($param['status']) && $param['status']) $updateArray['status']  = $param['status'];

		if (isset($param['remarks']) && $param['remarks']) $updateArray['remarks'] = $param['remarks'];

		$res = $this->where('id', $param['id'])->update($updateArray);

		model('Actionlog')->actionLog(session('manage_username'),'审核任务：'.$param['id'],1);

		if (!$res) return '提交失败';

		if (isset($param['status']) && $param['status']){
			//审核未通过
			switch($param['status']){
				case 5://撤销
					$info 		= $this->where(array(['id','=',$param['id']]))->find();
					//会员发布的
					if($info['uid']){
						//任务完成了几次
						$count = model('UserTask')->where(array(['task_id','=',$param['id']],['status','=',3]))->count();

						$r_number = $info['total_number']	-	$count;

						if($r_number > 0){
							$userinfo	= model('Users')->field('ly_users.id,ly_users.username,ly_users.sid,user_total.balance')->join('user_total','ly_users.id=user_total.uid')->where('ly_users.id', $info['uid'])->find();
							if($userinfo){

								$total_price		=	$r_number*$info['reward_price']	+ $r_number*$info['reward_price']*($info['pump']/100);

								if($total_price>0){

									$is_up_to = model('UserTotal')->where('uid', $userinfo['id'])->setInc('balance', $total_price);

									if(!$is_up_to){
										$this->where(array(['id','=',$param['id']],['status','=',2]))->update(array('status'=>1));//审核中
										return '提交失败';
									}

									// 流水
									$financial_data_p['uid'] 					= $userinfo['id'];
									$financial_data_p['username'] 				= $userinfo['username'];
									$financial_data_p['order_number'] 			= $info['order_number'];
									$financial_data_p['trade_number'] 			= 'L'.trading_number();;
									$financial_data_p['trade_type'] 			= 10;
									$financial_data_p['trade_before_balance']	= $userinfo['balance'];
									$financial_data_p['trade_amount'] 			= $total_price;
									$financial_data_p['account_balance'] 		= $userinfo['balance'] + $total_price;
									$financial_data_p['remarks'] 				= config($info['lang'].'.lang')[159];
									$financial_data_p['types'] 					= 1;	// 用户1，商户2
									model('common/TradeDetails')->tradeDetails($financial_data_p);
								}
							}
						}
					}

				break;
				case 2:
					$info 		= $this->where(array(['id','=',$param['id']],['status','=',2]))->find();
					if(!$info){
						$this->where(array(['id','=',$param['id']],['status','=',2]))->update(array('status'=>1));//审核中
						return '提交失败';
					}

					$userinfo	= model('Users')->field('ly_users.id,ly_users.username,ly_users.sid,user_total.balance')->join('user_total','ly_users.id=user_total.uid')->where('ly_users.id', $info['uid'])->find();
					if($userinfo){

						$total_price		=	$info['total_price']	+	$info['task_pump'];

						if($total_price>0){

							$is_up_to = model('UserTotal')->where('uid', $userinfo['id'])->setInc('balance', $total_price);

							if(!$is_up_to){
								$this->where(array(['id','=',$param['id']],['status','=',2]))->update(array('status'=>1));//审核中
								return '提交失败';
							}

							// 流水
							$financial_data_p['uid'] 					= $userinfo['id'];
							$financial_data_p['username'] 				= $userinfo['username'];
							$financial_data_p['order_number'] 			= $info['order_number'];
							$financial_data_p['trade_number'] 			= 'L'.trading_number();;
							$financial_data_p['trade_type'] 			= 10;
							$financial_data_p['trade_before_balance']	= $userinfo['balance'];
							$financial_data_p['trade_amount'] 			= $total_price;
							$financial_data_p['account_balance'] 		= $userinfo['balance'] + $total_price;
							$financial_data_p['remarks'] 				= config($info['lang'].'.lang')[159];
							$financial_data_p['types'] 					= 1;	// 用户1，商户2
							model('common/TradeDetails')->tradeDetails($financial_data_p);
						}
					}

				break;
			}
		}



		return 1;
	}
	/**
		任务订单审核
	**/

	public function userTaskAudit(){

		if (!request()->isAjax()) return '提交失败1';
		$param = input('param.');

		if (!$param) return '提交失败2';
		/**
		 * 批量审核
		 */
//		if (isset($param['data']) && is_array($param['data'])) return $this->userTaskBatchAudit($param);

		if (!$param || !isset($param['id']) || !$param['id']) return '提交失败2';

        $userTaskId = $param['id'];

		$updateArray = [];

		if (isset($param['status']) && $param['status']) $updateArray['status']  = $param['status'];

		if (isset($param['handle_remarks']) && $param['handle_remarks']) $updateArray['handle_remarks'] = $param['handle_remarks'];//说明

		$nowTime	= time();

		$updateArray['handle_time']		= $nowTime;

		$updateArray['complete_time']	= $nowTime;

        $info = model('api/UserTask')->where(array(['id','=',$userTaskId]))->find();

		if (!$info) return '该订单数据找不到';

        $task = model('api/Task')->where(array(['id','=',$info['task_id']]))->find();

        if ($task['status']>=0){
            $existGoods=db('users_goods')->where(['nft_id'=>$task['nft_id'],'uid'=>$task['uid'],'status'=>1])->find();
            if (empty($existGoods)){
                return '碎片不存在'.$task['nft_id'];
            }
            if ($existGoods['total_number']<$info['number']){
                return '卖出碎片的数量不足';
            }
        }

        $userinfo		= model('Users')->field('ly_users.id,ly_users.vip_level,ly_users.username,ly_users.sid,user_total.balance')->join('user_total','ly_users.id=user_total.uid')->where('ly_users.id', $info['uid'])->find();

        if (!$userinfo) return 'NFT碎片用户找不到';

		if (isset($param['status']) && $param['status']){//审核
			$UserDailydata	=	array();
			switch($updateArray['status']){
				case 3://完成
                    $ret = model('api/Task')->buyUserTaskDone($userTaskId);
					//更新每日完成任务次数
					$UserDailydata = array(
						'uid'				=>	$userinfo['id'],
						'username'			=>	$userinfo['username'],
						'field'				=>	'w_t_o_n',//完成
						'value' 			=>	1,
					);
				break;
				case 4://驳回
					//更新每日失败任务次数
					$UserDailydata = array(
						'uid'				=>	$userinfo['id'],
						'username'			=>	$userinfo['username'],
						'field'				=>	's_t_o_n',//失败
						'value' 			=>	1,
					);
				break;
				case 5://恶意
					//更新每日恶意任务次数
					$UserDailydata = array(
						'uid'				=>	$userinfo['id'],
						'username'			=>	$userinfo['username'],
						'field'				=>	'e_t_o_n',//恶意
						'value' 			=>	1,
					);
				break;
			}

//			if($UserDailydata){
//				model('UserDaily')->updateReportfield($UserDailydata);
//			}

            if($param['status']==3){
                $res = model('UserTask')->where(array(['id','=',$param['id']],['status','=',2]))->update($updateArray);//状态2 审核中的订单才能审核
            }else if($param['status']==2){
                $res = model('UserTask')->where(array(['id','=',$param['id']],['status','=',2]))->update($updateArray);//状态2 审核中的订单才能审核
            }

//            if (!$res) return '审核提交失败';
		}
		model('Actionlog')->actionLog(session('manage_username'),'审核碎片买卖订单：'.$param['id'],1);

		return 1;
	}

	/**
	 * 批量审核
	 */
	public function userTaskBatchAudit($param=[]){
		if (!$param) return '提交失败';

		foreach ($param['data'] as $key => $value) {
			$updateArray = [];
			$UserDailydata	=	array();
			if (isset($param['status']) && $param['status']) $updateArray['status']  = $param['status'];
			$updateArray['handle_time']		= time();
			$updateArray['complete_time']	= time();

			$task_info = model('UserTask')->field('task.order_number,task.lang,task.reward_price,task.total_number,ly_user_task.status,ly_user_task.uid,ly_user_task.task_id')->join('task','task.id=ly_user_task.task_id')->where('ly_user_task.id',$value['id'])->find();//完成

			if (!$task_info) continue;

			$userinfo		= model('Users')->field('ly_users.id,ly_users.vip_level,ly_users.username,ly_users.sid,user_total.balance')->join('user_total','ly_users.id=user_total.uid')->where('ly_users.id', $task_info['uid'])->find();

			if (!$userinfo) continue;


			if($param['status']==2){
				$res = model('UserTask')->where(array(['id','=',$value['id']],['status','=',4]))->update($updateArray);//状态2 审核中的订单才能审核
			}else{
				$res = model('UserTask')->where(array(['id','=',$value['id']],['status','=',2]))->update($updateArray);//状态2 审核中的订单才能审核
			}

			if (!$res) continue;

			if (isset($param['status']) && $param['status']){//审核


				switch($updateArray['status']){
					case 3: // 完成
						//任务提成
						$commission = $task_info['reward_price'];//任务单价
						if($commission > 0){
							//加余额钱
							$is_up_to = model('UserTotal')->where('uid', $userinfo['id'])->setInc('balance', $commission);

							if(!$is_up_to){
								model('UserTask')->where('id', $value['id'])->update(['status'=>2]);//变审核
								break;
							}
							//加总金额

							// 流水
							$financial_data_p['uid']                  = $userinfo['id'];
							$financial_data_p['sid']                  = $userinfo['sid'];
							$financial_data_p['username']             = $userinfo['username'];
							$financial_data_p['order_number']         = $task_info['order_number'];
							$financial_data_p['trade_number']         = 'L'.trading_number();
							$financial_data_p['trade_type']           = 6;
							$financial_data_p['trade_before_balance'] = $userinfo['balance'];
							$financial_data_p['trade_amount']         = $commission;
							$financial_data_p['account_balance']      = $userinfo['balance'] + $commission;
							$financial_data_p['remarks']              = config($task_info['lang'].'.lang')[160];
							$financial_data_p['types']                = 1;	// 用户1，商户2

							model('common/TradeDetails')->tradeDetails($financial_data_p);
							//上级返点
							if($userinfo['sid'] && $userinfo['vip_level']>1){
								$rebatearr = array(
									'num'			=>	1,
									'uid'			=>	$userinfo['id'],
									'sid'			=>	$userinfo['sid'],
									'order_number'	=>	$task_info['order_number'],
									'commission'	=>	$commission,
									'lang'			=>	$task_info['lang'],
								);

								$this->setrebate($rebatearr);
							}
						}
						//已经完成的 和 总的任务数 一样 更新任务 完成

						$y_surplus_number =	model('UserTask')->where(array(['task_id','=',$task_info['task_id']],['status','=',3]))->count();

						if($y_surplus_number == $task_info['total_number']){
							$this->where(array(['id','=',$task_info['task_id']],['status','=',3]))->update(['status'=>4, 'complete_time'=>time()]);
						}

						//更新每日恶意任务次数
						$UserDailydata = array(
							'uid'				=>	$userinfo['id'],
							'username'			=>	$userinfo['username'],
							'field'				=>	'w_t_o_n',//完成
							'value' 			=>	1,
						);
					break;
					case 4:
						$this->where('id', $task_info['task_id'])->dec('surplus_number')->inc('receive_number')->update();
						//更新每日恶意任务次数
						$UserDailydata = array(
							'uid'				=>	$userinfo['id'],
							'username'			=>	$userinfo['username'],
							'field'				=>	's_t_o_n',//失败
							'value' 			=>	1,
						);
					break;
					case 5:
						//更新每日恶意任务次数
						$UserDailydata = array(
							'uid'				=>	$userinfo['id'],
							'username'			=>	$userinfo['username'],
							'field'				=>	'e_t_o_n',//恶意
							'value' 			=>	1,
						);
						$this->where('id', $task_info['task_id'])->dec('surplus_number')->inc('receive_number')->update();

					break;
				}
				if($UserDailydata){
					model('UserDaily')->updateReportfield($UserDailydata);
				}
			}
			model('Actionlog')->actionLog(session('manage_username'),'审核订单：'.$value['id'],1);
		}

		return 1;
	}

	//返点
	public function setrebate($param){
		if($param['num']<4){//上三级

			$rebate			=	model('Setting')->where('id',1)->value('rebate'.$param['num']);//返点值
			if($rebate){

				$rebate_amount	=	round($param['commission']*($rebate/100),2);

				if($rebate_amount>0){
					$userinfo = model('Users')->field('ly_users.id,ly_users.username,ly_users.vip_level,ly_users.sid,user_total.balance')->join('user_total','ly_users.id=user_total.uid')->where('ly_users.id', $param['sid'])->find();

					if($userinfo && $userinfo['vip_level'] >1){

						$is_up_to = model('UserTotal')->where('uid', $userinfo['id'])->setInc('balance', $rebate_amount);

						if($is_up_to){
							model('UserTotal')->where('uid', $userinfo['id'])->setInc('total_balance', $rebate_amount);
							// 流水
							$financial_data_p['uid'] 					= $userinfo['id'];
							$financial_data_p['sid']					= $param['uid'];
							$financial_data_p['username'] 				= $userinfo['username'];
							$financial_data_p['order_number'] 			= $param['order_number'];
							$financial_data_p['trade_number'] 			= 'L'.trading_number();
							$financial_data_p['trade_type'] 			= 5;
							$financial_data_p['trade_before_balance']	= $userinfo['balance'];
							$financial_data_p['trade_amount'] 			= $rebate_amount;
							$financial_data_p['account_balance'] 		= $userinfo['balance'] + $rebate_amount;
							$financial_data_p['remarks'] 				= config($param['lang'].'.lang')[161];
							$financial_data_p['types'] 					= 1;	// 用户1，商户2

							model('common/TradeDetails')->tradeDetails($financial_data_p);
						}
					}
					if($userinfo['sid']){
						$rebatearr = array(
							'num'			=>	$param['num']+1,
							'uid'			=>	$userinfo['id'],
							'sid'			=>	$userinfo['sid'],
							'order_number'	=>	$param['order_number'],
							'commission'	=>	$param['commission'],
							'lang'			=>	$param['lang'],
						);
						$this->setrebate($rebatearr);
					}
				}
			}
		}
	}

}
